home *** CD-ROM | disk | FTP | other *** search
- Path: news.rrz.uni-hamburg.de!rzdspc1!simmons
- From: simmons@rzdspc1.informatik.uni-hamburg.de (Geoffrey Simmons)
- Newsgroups: comp.lang.c,gnu.gcc.help
- Subject: Casting unsigned short as unsigned int -> Bus error
- Date: 5 Jan 96 15:57:33 GMT
- Organization: University of Hamburg -- Germany
- Message-ID: <simmons.820857453@rzdspc1>
- NNTP-Posting-Host: rzdspc1.informatik.uni-hamburg.de
-
- I have a pointer to a struct with a member declared as unsigned short:
-
- typedef struct {
- ...
- unsigned short myshort;
- ...
- } RecType;
-
- RecType *MyRec;
-
- Now when I try to dereference that member, casting it to (unsigned int),
- I get a bus error:
-
- unsigned int myint;
-
- myint = (unsigned int) MyRec->myshort; /* Bus error! */
-
- I'm using GCC 2.7.2 on a SparcStation running SunOS 4.1.4. I've made sure
- that the pointer points to valid data.
-
- Actually, I'd like to print the value using printf, but it crashes on printf
- with a bus error. Since printf always promotes shorts to ints, and since
-
- printf("%d\n", MyRec->myshort);
-
- produces a bus error like the code shown above, I assume that printf is
- crashing for the same reason that the code shown above crashes (whatever
- that may be). (It also crashes if I use printf("%hu\n", ...), or if I cast
- the argument of printf to (unsigned int) and use "%d\n").
-
- The bizarre thing about this is that the debugger (GDB 4.15.1 for
- sparc-sun-sunos4.1.4) has no problem dereferencing the struct member I'm
- trying to get, for example when I say:
-
- (gdb) printf "%d\n", MyRec->myshort
- 1000
-
- where 1000 is precisely the value I expect to see. Using the debugger, I
- can detect nothing that explains what's wrong; the data's all there, and
- the program can dereference the other members of the struct just fine. I'm
- quite baffled; can anyone seen where I've gone astray?
-
- Thanks,
- Geoff
-
- --
- Geoffrey Simmons | simmons@informatik.uni-hamburg.de | "Insert wise
- University of Hamburg | Phone: (++49 40) 54715-381 | and witty
- Vogt-Koelln-Str. 30 | Fax: (++49 40) 54715-385 | quotation
- D-22527 Hamburg, Germany | | here."
-